package com.eqxiu.ieditor.weixin.util;

import java.io.IOException;
import java.security.KeyStore;
import java.util.Arrays;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtils {

	public static final String CT_JSON = "application/json;charset=utf8";
	public static final String CT_FORM = "application/x-www-form-urlencoded";

	private static OkHttpClient client = getUnsafeOkHttpClient();

	public static String get(String url) throws IOException {
		Request request = new Request.Builder().url(url).get().build();
		try (Response response = client.newCall(request).execute()) {
			return response.body().string();
		}
	}

	public static String post(String url, String params) throws IOException {
		Request request = new Request.Builder().url(url)
				.post(RequestBody.create(MediaType.parse(CT_FORM), params)).build();
		try (Response response = client.newCall(request).execute()) {
			return response.body().string();
		}
	}

	public static String post(String url, String params, String contentType) throws IOException {
		Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse(contentType), params))
				.build();
		try (Response response = client.newCall(request).execute()) {
			return response.body().string();
		}
	}

	public static String post(String url, String params, Headers headers) throws IOException {
		Request request = new Request.Builder().url(url)
				.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), params)).build();
		try (Response response = client.newCall(request).execute()) {
			return response.body().string();
		}
	}

	public static OkHttpClient getUnsafeOkHttpClient() {
		try {

			TrustManagerFactory trustManagerFactory = TrustManagerFactory
					.getInstance(TrustManagerFactory.getDefaultAlgorithm());
			trustManagerFactory.init((KeyStore) null);
			TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
			if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
				throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
			}
			X509TrustManager trustManager = (X509TrustManager) trustManagers[0];

			SSLContext sslContext = SSLContext.getInstance("TLS");
			sslContext.init(null, new TrustManager[] { trustManager }, null);
			SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

			OkHttpClient client = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory, trustManager)
					.hostnameVerifier(
							(one, session) -> {
								return true;
							}
					).build();

			// // Install the all-trusting trust manager
			// final SSLContext sslContext = SSLContext.getInstance("SSL");
			// sslContext.init(null, trustAllCerts, new
			// java.security.SecureRandom());
			// // Create an ssl socket factory with our all-trusting manager
			// final SSLSocketFactory sslSocketFactory =
			// sslContext.getSocketFactory();
			//
			// OkHttpClient okHttpClient = new OkHttpClient.Builder()
			// .sslSocketFactory(sslSocketFactory, (X509TrustManager)
			// trustAllCerts[0])
			// .hostnameVerifier((one, session) -> {
			// return true;
			// }).build();
			//
			return client;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
